//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Globalization;



namespace CodePlex.GpsTracker.GpxSupport
{
    public class Coordinate
    {
        private double _latitude = 0;
        private double _longitude = 0;
        private double _altitude = 0;
        private DateTime _coordinateTime;

        public Coordinate()
        {
        }

        public double Altitude
        {
            get { return _altitude; }
            set { _altitude = value; }
        }	

        public double Latitude
        {
            get { return _latitude; }
            set { _latitude = value; }
        }

        public double Longitude
        {
            get { return _longitude; }
            set { _longitude = value; }
        }

        public DateTime CoordinateTime
        {
            get { return _coordinateTime; }
            set { _coordinateTime = value; }
        }
    }

    public class Gpx
    {
        private List<Coordinate> _waypoints = new List<Coordinate>();
        private List<Coordinate> _routepoints = new List<Coordinate>();

        private string _name = String.Empty;
        private string _description = String.Empty;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
	
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }


        public Gpx()
        {
        }

        public void AddWaypoint(Coordinate waypoint)
        {
            _waypoints.Add(waypoint);
        }

        public void AddRoutepoint(Coordinate routepoint)
        {
            _routepoints.Add(routepoint);
        }

        public void GetGpx(TextWriter output)
        {
            XmlTextWriter gpxXml = new XmlTextWriter(output);
            gpxXml.WriteStartDocument();
            gpxXml.WriteStartElement("gpx");
            gpxXml.WriteAttributeString("version", "1.1");
            gpxXml.WriteAttributeString("creator", "GPS Tracker for Windows Mobile, http://www.codeplex.com/Wiki/View.aspx?ProjectName=gpstracker");
            gpxXml.WriteElementString("name", _name);
            gpxXml.WriteElementString("desc", _description);

            // START: Waypoint
            if (_waypoints != null)
            {
                foreach (Coordinate c in _waypoints)
                {
                    gpxXml.WriteStartElement("wpt");
                    WriteWptType(gpxXml, c);
                    gpxXml.WriteEndElement();
                }
            }
            // END: Waypoint

            // START: Route
            if ((_routepoints != null) && (_routepoints.Count > 0))
            {
                gpxXml.WriteStartElement("rte");
                gpxXml.WriteElementString("name", _name);
                gpxXml.WriteElementString("desc", _description);
                // START: Route points
                foreach (Coordinate c in _routepoints)
                {
                    gpxXml.WriteStartElement("rtept");
                    WriteWptType(gpxXml, c);
                    gpxXml.WriteEndElement();
                }
                // END: Route points
                gpxXml.WriteEndElement();
            }
            // END: Route

            gpxXml.WriteEndElement();
        }

        private static void WriteWptType(XmlTextWriter gpxXml, Coordinate c)
        {
            gpxXml.WriteAttributeString("lat", c.Latitude.ToString(new CultureInfo("en-US")));
            gpxXml.WriteAttributeString("lon", c.Longitude.ToString(new CultureInfo("en-US")));
            gpxXml.WriteElementString("ele", c.Altitude.ToString(new CultureInfo("en-US")));
            gpxXml.WriteElementString("time", ConvertToUtcTimeString(c.CoordinateTime));
        }

        public static string ConvertToUtcTimeString(DateTime t)
        {
            TimeSpan offset = System.TimeZone.CurrentTimeZone.GetUtcOffset(t);
            string offsetString = "";
            if (offset.Hours == 0)
            {
                offsetString = "Z";
            }
            else if (offset.ToString().StartsWith("-"))
            {
                offsetString = offset.ToString().Substring(0, 6);
            }
            else
            {
                offsetString = "+" + offset.ToString().Substring(0, 5);
            }

            return t.ToUniversalTime().ToString("s") + offsetString;
        }
    }
}
